package com.dily.study.demo.test;

import com.alibaba.fastjson2.*;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * Date: 2022-04-29 星期五
 * Time: 13:54
 * Author: Dily_Su
 * Remark:
 * fastjson2
 */
public class FastJson2 {

    public static void main(String[] args) {
        /** String 转 JSONObject */
        String strObject = "{\"id\":123,\"name\":\"dily\"}";
        JSONObject jsonObject = JSON.parseObject(strObject);
        // 获取属性值  不同类型的值使用不同的get方法
        System.out.println(jsonObject.getIntValue("id"));
        // 123
        System.out.println(jsonObject.getString("name"));
        // dily

        /** String 转 JSONArray */
        String strArray = "[{\"id\":123,\"name\":\"dily\"},{\"id\":124,\"name\":\"dily2\"}]";
        JSONArray jsonArray = JSON.parseArray(strArray);
        // 获取数组中的对象  不同的类型使用不同的get方法
        System.out.println(jsonArray.getJSONObject(0));
        // {"id":123,"name":"dily"}
        System.out.println(jsonArray.getString(0));
        // {"id":123,"name":"dily"}

        /** JavaBean 转 JSONString */
        @Data
        @AllArgsConstructor
        class Product {
            private int id;
            private String name;
        }

        Product product = new Product(123, "dily");
        String str1 = JSON.toJSONString(product);
        System.out.println(str1);
        // {"id":1,"name":"test"}
        String str2 = JSON.toJSONString(product, JSONWriter.Feature.BeanToArray);
        System.out.println(str2);
        // [1,"test"]

        /** JavaBean 转 UTF8 编码的 byte[] */
        byte[] utf8JSONBytes = JSON.toJSONBytes(product);
        System.out.println(Arrays.toString(utf8JSONBytes));
        // [123, 34, 105, 100, 34, 58, 49, 44, 34, 110, 97, 109, 101, 34, 58, 34, 116, 101, 115, 116, 34, 125]

        /** JavaBean 转 JSONN 格式的 byte[] */
        byte[] jsonbBytes = JSONB.toBytes(product);
        System.out.println(Arrays.toString(jsonbBytes));
        // [-90, 75, 105, 100, 1, 77, 110, 97, 109, 101, 77, 116, 101, 115, 116, -91]
        byte[] arrayJsonbBytes = JSONB.toBytes(product, JSONWriter.Feature.BeanToArray);
        System.out.println(Arrays.toString(arrayJsonbBytes));
        // [-106, 1, 77, 116, 101, 115, 116]

        /** String 转 javaBean */
        Product product1 = JSON.parseObject(strObject, Product.class);
        System.out.println(product1);
        // Product(id=123, name=dily)

        /** UTF8 byte[] 转 JavaBean */
        byte[] utf8bytes = "{\"id\":123,\"name\":\"dily\"}".getBytes(StandardCharsets.UTF_8);
        Product product2 = JSON.parseObject(utf8bytes, Product.class);
        System.out.println(product2);
        // Product(id=123, name=dily)

        /** JSONB byte[] 转 JavaBean */
        Product product3 = JSONB.parseObject(jsonbBytes, Product.class);
        System.out.println(product3);
        // Product(id=1, name=test)

        /** JSONPath */
        // 读取数据
        // strObject -> {"id":123,"name":"dily"}
        System.out.println(strObject);
        JSONReader reader = JSONReader.of(strObject); // 只能用一次，第二次使用必须重新赋值
        JSONPath idPath = JSONPath.of("$.id");
        JSONPath namePath = JSONPath.of("$.name");
        Object name = namePath.extract(reader);
        System.out.println(name);
        // dily
        reader = JSONReader.of(strObject);
        Object id = idPath.extract(reader);
        System.out.println(id);
        // 123

        // 读取 byte[] utf8Bytes
        JSONPath path = JSONPath.of("$.id"); // 缓存起来重复使用能提升性能
        JSONReader parser = JSONReader.of(utf8bytes);
        Object result1 = path.extract(parser);
        System.out.println(result1);
        // 123

        // 读取 byte[] jsonbBytes
        JSONReader jsonReader = JSONReader.ofJSONB(jsonbBytes); // 注意，这是利用ofJSONB方法
        Object result2 = path.extract(jsonReader);
        System.out.println(result2);
        // 123
    }

}
